package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account")
@SuppressWarnings({"all"})
public class UserAccountApiController {

    @Autowired
    private UserAccountService userAccountService;


    /**
     * 获取当前登录用户账户可用余额
     *
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "获取当前登录用户账户可用余额")
    @GetMapping("/userAccount/getAvailableAmount")
    public Result<BigDecimal> getAvailableAmount() {
        BigDecimal availableAmount = userAccountService.getAvailableAmount();
        return Result.ok(availableAmount);
    }


    /**
     * 用户余额支付订单-验证扣减账户余额
     * TODO:跟虚拟物品发货接口保持一致，用户ID通过入参对象传递。将来采用微信/支付宝支付后，支付宝会有支付成功回调->请求商户系统不会携带Token访问
     *
     * @param accountDeductVo
     * @return 业务调用正常返回业务状态码：200，反之业务失败，触发全局事务回滚
     */
    @Operation(summary = "用户余额支付订单-验证扣减账户余额")
    @PostMapping("/userAccount/checkAndDeduct")
    public Result checkAndDeduct(@RequestBody AccountDeductVo accountDeductVo) {
        userAccountService.checkAndDeduct(accountDeductVo);
        return Result.ok();
    }

}

